﻿using Studio77.GuessTheWordsWin8.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Studio77.GuessTheWordsWin8.Utils
{
    class LocalDataUtil
    {
        public static void SaveData(string key, object value)
        {
            ApplicationData.Current.LocalSettings.Values[key] = value;
        }

        public static object GetData(string key)
        {
            return ApplicationData.Current.LocalSettings.Values[key];
        }

        public static void RemoveData(string key)
        {
            ApplicationData.Current.LocalSettings.Values.Remove(key);
        }

        public static bool IsSetData(string key)
        {
            return ApplicationData.Current.LocalSettings.Values.ContainsKey(key);
        }

        static ObservableCollection<Type> _Types;
        static string _FileName = "LocalDataPage-list_data.xml";

        public static async Task<object> XMLDeserialize(Type type)
        {
            StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;

            StorageFile newFile = await folder.GetFileAsync(_FileName);
            Stream newFileStream = await newFile.OpenStreamForReadAsync();
            //进行反序列化  
            DataContractSerializer ser = new DataContractSerializer(type, GetTypes());
            object instance = ser.ReadObject(newFileStream);
            newFileStream.Dispose();
            return instance;
        }

        public async static Task<bool> ExistFile()
        {
            try
            {
                StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile file = await folder.GetFileAsync(_FileName);
                return true;
            }
            catch
            {
                return false;
            }

        }

        public async static Task XMLSerialize(object instance, Type type)
        {
            try
            {
                //取得当前程序存放数据的目录  
                StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
                //创建文件，如果文件存在就覆盖  
                StorageFile newFile = await folder.CreateFileAsync(_FileName, CreationCollisionOption.ReplaceExisting);
                //将内容序列化至文件  
                Stream newFileStream = await newFile.OpenStreamForWriteAsync();
                DataContractSerializer ser = new DataContractSerializer(type, GetTypes());
                ser.WriteObject(newFileStream, instance);
                newFileStream.Dispose();
            }
            catch
            { }
        }

        private static ObservableCollection<Type> GetTypes()
        {
            //添加要序列化的类型  
            if (_Types == null)
            {
                _Types = new ObservableCollection<Type>();
                _Types.Add(typeof(StageData));
                _Types.Add(typeof(MatrixCharInfo));
            }
            return _Types;
        }


    }
}
